#!python
#
# Info for generating core classes
#
#import pyplusplus.module_builder.call_policies as cp
from pyplusplus.module_builder.call_policies import *
import pygccxml.declarations.type_traits as tt


# --- Helper methods ----- #
def genFCFile(coreInfo):
   #global fileCounter
   #global classToFileMap
   fileCounter += 1
   
   subst_dict = {'ClassName' : coreInfo[0],
                 'BaseClass' : coreInfo[1],
		 'BaseExcludesAndPolicies' : genExcludesAndPolicies('base_c',coreInfo[2],coreInfo[3], "   "),
		 'ClassExcludesAndPolicies' : genExcludesAndPolicies('c',coreInfo[4],coreInfo[5]),
                 'BasePysteFileName' : classToFileMap[coreInfo[1]],
                 'Includes' : coreInfo[6],
                 'HasBase' : coreInfo[7],
                 'FileCounter' : fileCounter }
   file_contents = """#!python
#
#  GENERATED FILE -- DO NOT EDIT -- GENERATED FILE -- DO NOT EDIT
#  GenSortOrder:%(FileCounter)s
# Pyste file for %(ClassName)s class
#
for inc in %(Includes)s:
   Include(inc)
Include("OsgPtrHelpers.h")
Import("%(BasePysteFileName)s")

if %(HasBase)s:
   base_c = Class('osg::%(ClassName)sBase', 'OpenSG/OSG%(ClassName)sBase.h')
   final(base_c)
   %(BaseExcludesAndPolicies)s
   set_policy(base_c.getType, return_internal_reference(1))
   set_policy(base_c.getClassType, return_internal_reference(1))

c = Class('osg::%(ClassName)s', 'OpenSG/OSG%(ClassName)s.h')
holder(c, lambda x: "osg::%(ClassName)sRefPtr")
final(c)
%(ClassExcludesAndPolicies)s
set_policy(c.getType, return_internal_reference(1))
set_policy(c.getClassType, return_internal_reference(1))
exclude(c.getClass)
exclude(c.getStaticClass)

module_code(\"\"\"
    register_ptr_to_python<osg::%(ClassName)sPtr>();
    implicitly_convertible<osg::%(ClassName)sRefPtr, osg::%(ClassName)sPtr>();
    implicitly_convertible<osg::%(ClassName)sPtr, osg::%(BaseClass)sPtr>();
    
    def("RefPtr", &pyopensg::ToRefPtr<osg::%(ClassName)sPtr>);
    def("FCPtr", &pyopensg::ToFcPtr<osg::%(ClassName)sPtr>);
\"\"\")
""" % subst_dict

   file_name = 'gen_%s.pyste'% coreInfo[0]
   classToFileMap[coreInfo[0]] = file_name

   old_contents = ""
   try:
      old_file = file(file_name, 'r')
      old_contents = old_file.read()
   except:
      pass
   
   if old_contents != file_contents:      
      print "Generating: ", file_name
      pyste_file = file(file_name, 'w')
      pyste_file.write(file_contents)
      pyste_file.close()
   
   

# ------------ Data output and generation --------------- #
# Idea: Generate this from doxygen or opensg fcd files

class ClassInfo:
   def __init__(self, name, excludeList=[], policyList=[], includes=[]):
      self.name = name
      self.excludes = excludeList
      self.policies = policyList
      self.includes = includes

gen_classes = []

ac = ClassInfo("Action")
gen_classes.append(ac)

ac = ClassInfo("DrawActionBase", [], [], [])
gen_classes.append(ac)

ac = ClassInfo("DrawAction", [], [], [])
gen_classes.append(ac)

ac = ClassInfo("RenderActionBase", [], [], [])
gen_classes.append(ac)

ac = ClassInfo("RenderAction", [], [], [])
gen_classes.append(ac)

ac = ClassInfo("IntersectAction", [], [], [])
gen_classes.append(ac)

ra = ClassInfo("RenderAction")
gen_classes.append(ra)

#sfh = ClassInfo("SceneFileHandler",
#                ["getFileType","getReadProgressCB","getPathHandler",
#             "getDefaultGraphOp","getOptions"

ssm = ClassInfo("SimpleSceneManager")
gen_classes.append(ssm)

sc = ClassInfo("StatCollector",["getElem","reset"])
gen_classes.append(sc)


# --- Node Core's ------ #

class CoreInfo:
   def __init__(self, name, parent, excludeList=[], policyList=[], includes=[], hasBase=True):
      " If parent == None, then no parent. "
      self.name = name
      self.parent = parent
      self.excludes = excludeList
      self.policies = policyList
      self.includes = includes
      self.hasBase = hasBase

# Map from class name to CoreInfo class
core_infos = {}

# fc's that are custom (not in fcd files)
aci = CoreInfo("AttachmentContainer", "FieldContainer", hasBase=False)
core_infos["AttachmentContainer"] = aci

ai = CoreInfo("Attachment", "FieldContainer", hasBase=False)
core_infos["Attachment"] = ai

ni = CoreInfo("Node", "AttachmentContainer", hasBase=False)
core_infos["Node"] = ni

nci = CoreInfo("NodeCore", "AttachmentContainer", hasBase=False)
core_infos["NodeCore"] = nci

# fc's that need customized
image_info = CoreInfo("Image","AttachmentContainer",
                      ["store","restore","getData","getDataByTime"])
core_infos["Image"] = image_info

window_info = CoreInfo("Window","AttachmentContainer",
                      ["getFunction","getFunctionNoCheck"])
core_infos["Window"] = window_info

# Core data
# [ Class Name, Base Name,
#  [ List of base class excludes ], [List of base class policies],
#  [ List of derived class excludes ], [List of derived class policies],
#  [ Includes ],
#  <has_base> ]
#
#  Has base is true when type is one of the ones generated by fcdedit
# and thus it has a base class (ClassBase).
#
gen_data = []

attachment_container_info = ["AttachmentContainer", "FieldContainer",
   [],
   [],
   [],
   [],
   [],
   False ]
gen_data.append(attachment_container_info)

attachment_info = ["Attachment", "FieldContainer",
   [],
   [],
   [],
   [],
   [],
   False ]
gen_data.append(attachment_info)

attachment_info = ["Node", "AttachmentContainer",
   [],
   [],
   [],
   [],
   [],
   False ]
gen_data.append(attachment_info)
   
# ------ CORES -------- #
node_core_info = ["NodeCore", "AttachmentContainer",
   [],
   [],
   [],
   [],
   [],
   False ]   
gen_data.append(node_core_info)

group_core_info = ["Group", "NodeCore",
    [],
    [],
    [],
   [],
    [], True ]
gen_data.append(group_core_info)

transform_core_info = ["Transform", "Group",
    [],
    [],
    [],
    [],
    [], True ]
gen_data.append(transform_core_info)

billboard_core_info = ["Billboard", "Group",
    [],
    [],
    [],
   [],
    [], True ]
gen_data.append(billboard_core_info)

distlod_core_info = ["DistanceLOD", "Group",
    [],
    [],
    [],
   [],
    [], True ]
gen_data.append(distlod_core_info)

invtrans_core_info = ["InverseTransform", "Group",
    [],
    [],
    [],
   [],
    [], True ]
gen_data.append(invtrans_core_info)

matgroup_core_info = ["MaterialGroup", "Group",
    [],
    [],
    [],
   [],
    [], True ]
gen_data.append(matgroup_core_info)

switch_core_info = ["Switch", "Group",
    [],
    [],
    [],
   [],
    [], True ]
gen_data.append(switch_core_info)

comp_xform_core_info = ["ComponentTransform", "Transform",
    [],
    [],
    [],
   [],
    [], True ]
gen_data.append(comp_xform_core_info)

drawable_core_info = ["Drawable", "NodeCore",
    [],
    [],
    [],
   [],
    [], True ]
gen_data.append(drawable_core_info)


matdrawable_core_info = ["MaterialDrawable", "Drawable",
    [],
    [],
    [],
   [],
    [], True ]
gen_data.append(matdrawable_core_info)

geom_core_info = ["Geometry", "MaterialDrawable",
    [],
    [],
    [],
    [#('getProperty',return_value_policy(copy_const_reference)),
    ], 
   [],
    True ]
gen_data.append(geom_core_info)

#particles_core_info = ["Particles", "MaterialDrawable",
#    ['getSFMode','getSFPositions','getMFSizes','getSFSecPositions','getSFColors','getSFnormals',
#     'getMFIndices','getMFTextureZs','getSFDrawOrder','getSFDynamic','getSFBsp'],
#    [],
#    [],
#   [],
#    [] ]
#gen_data.append(particles_core_info)

if True:
   slices_core_info = ["Slices", "MaterialDrawable",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(slices_core_info)
   
   light_core_info = ["Light", "NodeCore",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(light_core_info)
   
   dirlight_core_info = ["DirectionalLight", "Light",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(dirlight_core_info)
   
   ptlight_core_info = ["PointLight", "Light",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(ptlight_core_info)
   
   spotlight_core_info = ["SpotLight", "PointLight",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(spotlight_core_info)
   
   
   # -------- Windows and viewports ------- #
   window_info = ["Window", "AttachmentContainer",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(window_info)
   
   glut_window_info = ["GLUTWindow", "Window",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(glut_window_info)
   
   passive_win_info = ["PassiveWindow", "Window",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(passive_win_info)
   
   
   cluster_win_info = ["ClusterWindow", "Window",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(cluster_win_info)
   
   
   md_win_info = ["MultiDisplayWindow", "ClusterWindow",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(md_win_info)
   
   
   sf_win_info = ["SortFirstWindow", "ClusterWindow",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(sf_win_info)
   
   # Viewports
   vp_info = ["Viewport", "AttachmentContainer",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(vp_info)
   
   
   cb_vp_info = ["ColorBufferViewport", "Viewport",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(cb_vp_info)
   
   passive_vp_info = ["PassiveViewport", "Viewport",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(passive_vp_info)
   
   
   stereo_vp_info = ["StereoBufferViewport", "Viewport",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(stereo_vp_info)
   
   
   
   # -------- Materials --------- #
   material_info = ["Material", "AttachmentContainer",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(material_info)
   
   chunk_material_info = ["ChunkMaterial", "Material",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_material_info)
   
   fres_material_info = ["FresnelMaterial", "Material",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(fres_material_info)
   
   
   phong_material_info = ["PhongMaterial", "Material",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(phong_material_info)
   
   
   simp_material_info = ["SimpleMaterial", "Material",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(simp_material_info)
   
   st_material_info = ["SimpleTexturedMaterial", "SimpleMaterial",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(st_material_info)
   
   mp_material_info = ["MultiPassMaterial", "Material",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(mp_material_info)
   
   
   image_info = ["Image", "AttachmentContainer",
       [],
       [],
       ["store","restore","getData","getDataByTime"],
       [],
       [], True ]
   gen_data.append(image_info)
   
   
   # ------- Chunks ------- #
   chunk_info = ["StateChunk", "Attachment",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
   
   chunk_info = ["BlendChunk", "StateChunk",
       [],
       [],
       [],
       [], 
       [],
       True ]
   gen_data.append(chunk_info)
   
   chunk_info = ["ClipPlaneChunk", "StateChunk",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
   
   chunk_info = ["ColorMaskChunk", "StateChunk",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
   
   chunk_info = ["DepthChunk", "StateChunk",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
   
   
   chunk_info = ["LightChunk", "StateChunk",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
   
   
   chunk_info = ["LineChunk", "StateChunk",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
       
   
   chunk_info = ["MaterialChunk", "StateChunk",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
       
   
   chunk_info = ["PointChunk", "StateChunk",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
       
   
   chunk_info = ["PolygonChunk", "StateChunk",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(chunk_info)
   
   
   chunk_info = ["ProgramChunk", "StateChunk",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(chunk_info)
   
   chunk_info = ["FragmentProgramChunk", "ProgramChunk",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
   
   chunk_info = ["VertexProgramChunk", "ProgramChunk",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
   
   chunk_info = ["ShaderParameterChunk", "StateChunk",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
   
   
   chunk_info = ["ShaderChunk", "ShaderParameterChunk",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(chunk_info)
   
   
   chunk_info = ["SHLChunk", "ShaderChunk",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(chunk_info)
   
   
   chunk_info = ["StencilChunk", "StateChunk",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(chunk_info)
   
   chunk_info = ["TexGenChunk", "StateChunk",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(chunk_info)
   
   chunk_info = ["TextureChunk", "StateChunk",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(chunk_info)
   
   
   chunk_info = ["CubeTextureChunk", "TextureChunk",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(chunk_info)
   
   chunk_info = ["TwoSidedLightingChunk", "StateChunk",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(chunk_info)
   
   
   sparam_info = ["ShaderParameter", "AttachmentContainer",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(sparam_info)
   
   sparam_info = ["ShaderParameterBool", "ShaderParameter",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(sparam_info)
   
   sparam_info = ["ShaderParameterInt", "ShaderParameter",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(sparam_info)
   
   sparam_info = ["ShaderParameterMatrix", "ShaderParameter",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(sparam_info)
   
   sparam_info = ["ShaderParameterReal", "ShaderParameter",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(sparam_info)
   
   sparam_info = ["ShaderParameterString", "ShaderParameter",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(sparam_info)
   
   sparam_info = ["ShaderParameterVec2f", "ShaderParameter",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(sparam_info)
   
   sparam_info = ["ShaderParameterVec3f", "ShaderParameter",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(sparam_info)
   
   sparam_info = ["ShaderParameterVec4f", "ShaderParameter",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(sparam_info)
   
   # ---- Cameras, backgrounds, etc. ----- #
   background_info = ["Background", "AttachmentContainer",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(background_info)
   
   depth_clear_bg_info = ["DepthClearBackground", "Background",
       [],
       [],
       [],
       [],
       [], 
       True ]
   gen_data.append(depth_clear_bg_info)
   
   grad_bg_info = ["GradientBackground", "Background",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(grad_bg_info)
   
   
   image_bg_info = ["ImageBackground", "Background",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(image_bg_info)
   
   passive_bg_info = ["PassiveBackground", "Background",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(passive_bg_info)
   
   
   poly_bg_info = ["PolygonBackground", "Background",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(poly_bg_info)
   
   
   sky_bg_info = ["SkyBackground", "Background",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(sky_bg_info)
   
   
   solid_bg_info = ["SolidBackground", "Background",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(solid_bg_info)
   
   
   texgrab_bg_info = ["TextureGrabBackground", "Background",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(texgrab_bg_info)
   
   
   tex_bg_info = ["TextureBackground", "Background",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(tex_bg_info)
   
   # Cameras
   cam_info = ["Camera", "AttachmentContainer",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(cam_info)
   
   cam_info = ["CameraDecorator", "Camera",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(cam_info)
   
   
   cam_info = ["MatrixCameraDecorator", "CameraDecorator",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(cam_info)
   
   
   cam_info = ["StereoCameraDecorator", "CameraDecorator",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(cam_info)
   
   cam_info = ["ProjectionCameraDecorator", "StereoCameraDecorator",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(cam_info)
   
   
   cam_info = ["ShearedStereoCameraDecorator", "StereoCameraDecorator",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(cam_info)
   
   
   cam_info = ["TileCameraDecorator", "CameraDecorator",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(cam_info)
   
   cam_info = ["MatrixCamera", "Camera",
       [],
       [],
       [],
       [],
       [], 
       True ]
   gen_data.append(cam_info)
   
   
   cam_info = ["PerspectiveCamera", "Camera",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(cam_info)
   
   
   cam_info = ["OffCenterPerspectiveCamera", "PerspectiveCamera",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(cam_info)
   
   
   # Foreground
   fg_info = ["Foreground", "AttachmentContainer",
       [],
       [],
       [],
       [],
       [], 
       True ]
   gen_data.append(fg_info)
   
   
   fg_info = ["GrabForeground", "Foreground",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(fg_info)
   
   
   fg_info = ["FileGrabForeground", "GrabForeground",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(fg_info)
   
   
   fg_info = ["ImageForeground", "Foreground",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(fg_info)
   
   
   fg_info = ["PolygonForeground", "Foreground",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(fg_info)
   
   fg_info = ["StatisticsForeground", "Foreground",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(fg_info)
   
   
   fg_info = ["GraphicStatisticsForeground", "StatisticsForeground",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(fg_info)
   
   
   fg_info = ["SimpleStatisticsForeground", "StatisticsForeground",
       [],
       [],
       [],
       [],
       [], True ]
   gen_data.append(fg_info)
   
   
   
   fg_info = ["TextureGrabForeground", "Foreground",
       [],
       [],
       [],
      [],
       [], True ]
   gen_data.append(fg_info)










